package com.example.tellwin.question.act;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.apkfuns.logutils.LogUtils;
import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.CpBaseActivty;
import com.example.tellwin.base.DaggerRxComponent;
import com.example.tellwin.common.Common;
import com.example.tellwin.event.CallEvent;
import com.example.tellwin.event.IMEventBean;
import com.example.tellwin.event.OrderDetailEvent;
import com.example.tellwin.event.OrderListEvent;
import com.example.tellwin.mine.bean.OrderDetailBean;
import com.example.tellwin.question.contract.WriteBroadContract;
import com.example.tellwin.question.presenter.WriteBroadPresenter;
import com.example.tellwin.tic.TICClassroomOption;
import com.example.tellwin.tic.TICManager;
import com.example.tellwin.utils.ImageUtils;
import com.example.tellwin.utils.ToastUtil;
import com.example.tellwin.view.ListPopuwindow;
import com.example.tellwin.view.SeekBarPopuwindow;
import com.example.tellwin.view.TakePhotoDialog;
import com.google.gson.Gson;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMMessage;
import com.tencent.imsdk.v2.V2TIMValueCallback;
import com.tencent.teduboard.TEduBoardController;
import com.tencent.trtc.TRTCCloud;
import com.tencent.trtc.TRTCCloudDef;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WriteBroadActivity extends CpBaseActivty implements TICManager.TICIMStatusListener, TICManager.TICEventListener, WriteBroadContract.View {

    @BindView(R.id.broad_fl)
    FrameLayout broadFl;
    @BindView(R.id.microphone_iv)
    ImageView microphoneIv;
    @BindView(R.id.loudspeaker_iv)
    ImageView loudspeakerIv;
    @BindView(R.id.graffiti_iv)
    ImageView graffitiIv;
    @BindView(R.id.select_tool_iv)
    ImageView selectToolIv;
    @BindView(R.id.back_color_iv)
    ImageView backColorIv;
    @BindView(R.id.paint_color_iv)
    ImageView paintColorIv;
    @BindView(R.id.back_photo_iv)
    ImageView backPhotoIv;
    @BindView(R.id.paint_width_iv)
    ImageView paintWidthIv;
    @BindView(R.id.text_size_iv)
    ImageView textSizeIv;
    @BindView(R.id.text_style_iv)
    ImageView textStyleIv;
    @BindView(R.id.text_color_iv)
    ImageView textColorIv;
    @BindView(R.id.broad_back_iv)
    ImageView backIv;
    @BindView(R.id.reset_iv)
    ImageView resetIv;
    @BindView(R.id.add_broad_iv)
    ImageView addBroadIv;
    @BindView(R.id.previous_page_iv)
    ImageView previousPageIv;
    @BindView(R.id.next_page_iv)
    ImageView nextPageIv;
    @BindView(R.id.commit_btn)
    TextView commitBtn;

    TEduBoardController mBoard;
    TRTCCloud mTrtcCloud;
    protected TICManager mTicManager;
    private MyBoardCallback mBoardCallback;

    int classId = 100;
    String orderId;

    private boolean isOpenAudio;
    private boolean mEnableAudioRouteSpeaker = true;

    private TakePhotoDialog takePhotoDialog;


    final ToolType[] mToolMap = {
            new ToolType("鼠标", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_MOUSE),
            new ToolType("铅笔", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_PEN),
            new ToolType("橡皮", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_ERASER),
            new ToolType("直线", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_LINE),
            new ToolType("激光教鞭", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_LASER),
            new ToolType("空心椭圆", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_OVAL),
            new ToolType("空心矩形", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_RECT),
            new ToolType("实心椭圆", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_OVAL_SOLID),
            new ToolType("实心矩形", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_RECT_SOLID),
            new ToolType("点选", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_POINT_SELECT),
            new ToolType("框选", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_RECT_SELECT),
            new ToolType("文本", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_TEXT),
            new ToolType("手势缩放", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_ZOOM_DRAG),
            new ToolType("空心正方形", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_SQUARE),
            new ToolType("实心正方形", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_SQUARE_SOLID),
            new ToolType("空心正圆形", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_CIRCLE),
            new ToolType("实心正圆形", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_CIRCLE_SOLID),
            new ToolType("自定义图形", TEduBoardController.TEduBoardToolType.TEDU_BOARD_TOOL_TYPE_BOARD_CUSTOM_GRAPH)
    };

    final ToolType[] ColorMap = {
            new ToolType("白", 0xFFFFFFFF),
            new ToolType("红", 0xFFFF0000),
            new ToolType("橙", 0xFFFFA500),
            new ToolType("黄", 0xFFFFFF00),
            new ToolType("绿", 0xFF00FF00),
            new ToolType("青", 0xFF00FFFF),
            new ToolType("蓝", 0xFF0000ff),
            new ToolType("紫", 0xFF800080),
    };

    final ToolType[] TextStyle = {
            new ToolType("常规", TEduBoardController.TEduBoardTextStyle.TEDU_BOARD_TEXT_STYLE_NORMAL),
            new ToolType("粗体", TEduBoardController.TEduBoardTextStyle.TEDU_BOARD_TEXT_STYLE_BOLD),
            new ToolType("斜体", TEduBoardController.TEduBoardTextStyle.TEDU_BOARD_TEXT_STYLE_ITALIC),
            new ToolType("粗斜体", TEduBoardController.TEduBoardTextStyle.TEDU_BOARD_TEXT_STYLE_BOLD_ITALIC),
    };

    V2TIMValueCallback<V2TIMMessage> callback = new V2TIMValueCallback<V2TIMMessage>() {
        @Override
        public void onError(int i, String s) {

        }

        @Override
        public void onSuccess(V2TIMMessage v2TIMMessage) {
            mTicManager.quitClassroom(true, new TICManager.TICCallback() {
                @Override
                public void onSuccess(Object data) {
                    mTicManager.destroyClassroom(classId, null);
                    finish();
                    EventBus.getDefault().post(new OrderDetailEvent());
                    EventBus.getDefault().post(new OrderListEvent());
                }

                @Override
                public void onError(String module, int errCode, String errMsg) {
                    mTicManager.destroyClassroom(classId, null);
                    finish();
                    EventBus.getDefault().post(new OrderDetailEvent());
                    EventBus.getDefault().post(new OrderListEvent());
                }
            });
        }
    };

    @Inject
    WriteBroadPresenter mPresenter;
    private Runnable mTimeRunnable;
    private int mTimeCount;
    private Handler mTimeHandler;
    private HandlerThread mTimeHandlerThread;
    private String userId;//对面的人id；
    private OrderDetailBean orderDetailBean;

    private Handler uiHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 1001:
                    joinClass();
                    break;
            }
        }
    };

    List<String> broadNames = new ArrayList<>();
    List<String> photoPath = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_broad);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        orderId = getIntent().getStringExtra(Common.ORDER_ID);
        classId = getIntent().getIntExtra(Common.ClassId, 0);
        userId = getIntent().getStringExtra(Common.USER_ID);
        orderDetailBean = (OrderDetailBean) getIntent().getSerializableExtra(Common.ORDER);
        LogUtils.e(classId);
        mTimeHandlerThread = new HandlerThread("time-count-thread_1");
        mTimeHandlerThread.start();
        mTimeHandler = new Handler(mTimeHandlerThread.getLooper());
        initViews();
        showTimeCount();
        initTrtc();
        joinClass();
        initEvents();
        initDatas();
    }

    private void showTimeCount() {
        if (mTimeRunnable != null) {
            return;
        }
        mTimeCount = 0;
        if (mTimeRunnable == null) {
            mTimeRunnable = new Runnable() {
                @Override
                public void run() {
                    mTimeCount++;
                    mTimeHandler.postDelayed(mTimeRunnable, 1000);
                }
            };
        }
        mTimeHandler.postDelayed(mTimeRunnable, 1000);
    }

    @Override
    protected void initViews() {
        DaggerRxComponent.builder()
                .baseAppComponent(MainApplication.getAppComponent())
                .build()
                .inject(this);
        mPresenter.attachView(this);

        if (MainApplication.Identity.equals(Common.ANSWER) && orderDetailBean != null && orderDetailBean.getOrderFileList() != null) {
            for (int i = 0; i < orderDetailBean.getOrderFileList().size(); i++) {
                OrderDetailBean.OrderFileBean orderFileBean = orderDetailBean.getOrderFileList().get(i);
                if (orderFileBean.getFileType().equals("1")) {
                    String path = orderFileBean.getFileUrl();
                    photoPath.add(path);
                }
            }
        }

        mTicManager = MainApplication.getTICManager();
        //2.白板
        mBoard = mTicManager.getBoardController();

    }

    private void initTrtc() {
        mTicManager = MainApplication.getTICManager();
        mTicManager.addIMStatusListener(this);
        //1、获取trtc
        mTrtcCloud = mTicManager.getTRTCClound();

        if (mTrtcCloud != null) {
            isOpenAudio = true;
            mTrtcCloud.startLocalAudio();
        }
    }

    /**
     * 进入课堂
     */
    private void joinClass() {

        //1、设置白板的回调
        mBoardCallback = new MyBoardCallback();

        //2、如果用户希望白板显示出来时，不使用系统默认的参数，就需要设置特性缺省参数，如是使用默认参数，则填null。
        TEduBoardController.TEduBoardInitParam initParam = new TEduBoardController.TEduBoardInitParam();
        initParam.brushColor = new TEduBoardController.TEduBoardColor(255, 0, 0, 255);
        initParam.smoothLevel = 0; //用于指定笔迹平滑级别，默认值0.1，取值[0, 1]

        TICClassroomOption classroomOption = new TICClassroomOption();
        classroomOption.classId = classId;
        classroomOption.boardCallback = mBoardCallback;
        classroomOption.boardInitPara = initParam;

        mTicManager.joinClassroom(classroomOption, new TICManager.TICCallback() {
            @Override
            public void onSuccess(Object data) {
                LogUtils.e("进入课堂成功:" + classId);
            }

            @Override
            public void onError(String module, int errCode, String errMsg) {
                if (errCode == 10015) {
                    LogUtils.e("课堂不存在:" + classId + " err:" + errCode + " msg:" + errMsg);
                    ToastUtil.show(WriteBroadActivity.this, "课堂不存在，请重新解答！");
                    finish();
                } else {
                    ToastUtil.show(WriteBroadActivity.this, "进入课堂失败，请重新解答！");
                    LogUtils.e("进入课堂失败:" + classId + " err:" + errCode + " msg:" + errMsg);
                    finish();
                    // uiHandler.sendEmptyMessageAtTime(1001, 2000);
                }
            }
        });
    }

    @Override
    protected void initEvents() {

        mTicManager.addEventListener(this);
        mBoard.addCallback(mBoardCallback);

    }

    @Override
    protected void initDatas() {

    }


    @Override
    public void onTICForceOffline() {

    }

    @Override
    public void onTICUserSigExpired() {

    }

    @Override
    public void onTICUserVideoAvailable(String userId, boolean available) {

    }

    @Override
    public void onTICUserSubStreamAvailable(String userId, boolean available) {

    }

    @Override
    public void onTICUserAudioAvailable(String userId, boolean available) {

    }

    @Override
    public void onTICMemberJoin(List<String> userList) {

    }

    @Override
    public void onTICMemberQuit(List<String> userList) {

    }

    @Override
    public void onTICVideoDisconnect(int errCode, String errMsg) {

    }

    @Override
    public void onTICClassroomDestroy() {

    }

    @Override
    public void onTICSendOfflineRecordInfo(int code, String desc) {

    }

    @OnClick({R.id.microphone_iv, R.id.loudspeaker_iv, R.id.graffiti_iv, R.id.select_tool_iv, R.id.back_color_iv, R.id.paint_color_iv, R.id.back_photo_iv, R.id.paint_width_iv, R.id.text_size_iv, R.id.text_style_iv, R.id.text_color_iv, R.id.broad_back_iv, R.id.reset_iv, R.id.add_broad_iv, R.id.previous_page_iv, R.id.next_page_iv, R.id.commit_btn, R.id.clear_graffiti_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.microphone_iv:
                if (isOpenAudio) {
                    isOpenAudio = false;
                    mTrtcCloud.stopLocalAudio();
                    ToastUtil.show(this, "关闭麦克风");
                } else {
                    isOpenAudio = true;
                    mTrtcCloud.startLocalAudio();
                    ToastUtil.show(this, "打开麦克风");
                }
                break;
            case R.id.loudspeaker_iv:
                mEnableAudioRouteSpeaker = !mEnableAudioRouteSpeaker;
                mTrtcCloud.setAudioRoute(mEnableAudioRouteSpeaker ? TRTCCloudDef.TRTC_AUDIO_ROUTE_SPEAKER : TRTCCloudDef.TRTC_AUDIO_ROUTE_EARPIECE);
                ToastUtil.show(this, mEnableAudioRouteSpeaker ? "打开扬声器" : "关闭扬声器");
                break;
            case R.id.graffiti_iv:
                boolean isDrawEnable = mBoard.isDrawEnable();
                mBoard.setDrawEnable(!isDrawEnable);
                ToastUtil.show(this, !isDrawEnable ? "打开涂鸦" : "关闭涂鸦");
                break;
            case R.id.select_tool_iv:
                ListPopuwindow listPopupWindow = new ListPopuwindow(this, mToolMap, (position, toolType) -> mBoard.setToolType(toolType.type));
                listPopupWindow.show(selectToolIv);
                break;
            case R.id.back_color_iv:
                ListPopuwindow colorPpupWindow = new ListPopuwindow(this, ColorMap, (position, toolType) -> {
                    TEduBoardController.TEduBoardColor eduBoardColor = new TEduBoardController.TEduBoardColor(toolType.type);
                    mBoard.setBackgroundColor(eduBoardColor);
                });
                colorPpupWindow.show(backColorIv);
                break;
            case R.id.paint_color_iv:
                ListPopuwindow paintColorPpupWindow = new ListPopuwindow(this, ColorMap, (position, toolType) -> {
                    TEduBoardController.TEduBoardColor eduBoardColor = new TEduBoardController.TEduBoardColor(toolType.type);
                    mBoard.setBrushColor(eduBoardColor);
                });
                paintColorPpupWindow.show(paintColorIv);
                break;
            case R.id.back_photo_iv:
                if (takePhotoDialog == null) {
                    takePhotoDialog = new TakePhotoDialog(this);
                }
                takePhotoDialog.show();
                break;
            case R.id.paint_width_iv:
                SeekBarPopuwindow seekBarPopuwindow = new SeekBarPopuwindow(this, 120, mBoard.getBrushThin(), size -> {
                    mBoard.setBrushThin(size);
                });
                seekBarPopuwindow.show(paintWidthIv);
                break;
            case R.id.text_size_iv:
                // mBoard.getTextSize();
                SeekBarPopuwindow textSeekBarPopuwindow = new SeekBarPopuwindow(this, 500, 100, size -> {
                    mBoard.setTextSize(size);
                });
                textSeekBarPopuwindow.show(textSizeIv);
                break;
            case R.id.text_style_iv:
                ListPopuwindow textStylePpupWindow = new ListPopuwindow(this, TextStyle, (position, toolType) -> {
                    mBoard.setTextStyle(toolType.type);
                });
                textStylePpupWindow.show(textStyleIv);
                break;
            case R.id.text_color_iv:
                ListPopuwindow textColorPpupWindow = new ListPopuwindow(this, ColorMap, (position, toolType) -> {
                    TEduBoardController.TEduBoardColor eduBoardColor = new TEduBoardController.TEduBoardColor(toolType.type);
                    mBoard.setTextColor(eduBoardColor);
                });
                textColorPpupWindow.show(textColorIv);
                break;
            case R.id.broad_back_iv:
                mBoard.undo();
                break;
            case R.id.reset_iv:
                mBoard.reset();
                break;
            case R.id.add_broad_iv:
                String broadName = mBoard.addBoard(null);
                break;
            case R.id.previous_page_iv:
                mBoard.prevBoard();
                break;
            case R.id.next_page_iv:
                mBoard.nextBoard();
                break;
            case R.id.commit_btn:
                int callDuration = 0;
                if (mTimeCount <= 60) {
                    callDuration = 1;
                } else {
                    callDuration = mTimeCount % 60 == 0 ? mTimeCount / 60 : (mTimeCount / 60) + 1;
                }
                mPresenter.answerSuccess(orderId, "", callDuration + "");
                break;
            case R.id.clear_graffiti_iv:

                break;
        }
    }

    @Override
    public void answerSuccess() {
        IMEventBean callEvent = new IMEventBean();
        callEvent.type = Common.Answer_Success_Event;
        callEvent.orderId = orderId;
        callEvent.classId = classId;
        V2TIMManager.getInstance().sendC2CTextMessage(new Gson().toJson(callEvent), userId, callback);
    }

    @Subscribe
    public void onEvent(CallEvent event) {
        if (event.type == Common.Answer_Success_Event) {
            mTicManager.quitClassroom(true, new TICManager.TICCallback() {
                @Override
                public void onSuccess(Object data) {
                    mTicManager.destroyClassroom(classId, null);
                    finish();
                    EventBus.getDefault().post(new OrderDetailEvent());
                    EventBus.getDefault().post(new OrderListEvent());
                }

                @Override
                public void onError(String module, int errCode, String errMsg) {
                    mTicManager.destroyClassroom(classId, null);
                    finish();
                    EventBus.getDefault().post(new OrderDetailEvent());
                    EventBus.getDefault().post(new OrderListEvent());
                }
            });
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    public class MyBoardCallback implements TEduBoardController.TEduBoardCallback {

        @Override
        public void onTEBError(int code, String msg) {
            LogUtils.i("onTEBError:" + code + "|" + msg);
            ToastUtil.show(WriteBroadActivity.this, "白板初始化失败，重新解答");
            mTicManager.destroyClassroom(classId, null);
            finish();
        }

        @Override
        public void onTEBWarning(int code, String msg) {
            LogUtils.i("onTEBWarning:" + code + "|" + msg);
        }

        @Override
        public void onTEBInit() {
            View boardview = mBoard.getBoardRenderView();
            LogUtils.i("onTEBInit");
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT);
            layoutParams.gravity = Gravity.CENTER;
            broadFl.addView(boardview, layoutParams);
            ToastUtil.show(WriteBroadActivity.this, "白板初始化成功！");
            String curBroadId = mBoard.getCurrentBoard();
            broadNames.add(curBroadId);
            if (photoPath.size() > 0) {
                mBoard.setBackgroundImage(photoPath.get(0), TEduBoardController.TEduBoardImageFitMode.TEDU_BOARD_IMAGE_FIT_MODE_CENTER);
            }
        }

        @Override
        public void onTEBHistroyDataSyncCompleted() {

        }

        @Override
        public void onTEBSyncData(String data) {

        }


        @Override
        public void onTEBAddBoard(List<String> boardId, final String fileId) {
            LogUtils.i("onTEBAddBoard:" +  mBoard.getBoardList());
            broadNames = new ArrayList<>( mBoard.getBoardList());
            if (MainApplication.Identity.equals(Common.ANSWER)) {
                if (photoPath.size() >= broadNames.size()) {
                    String url = photoPath.get(broadNames.size() - 1);
                    LogUtils.e(url);
                    mBoard.setBackgroundImage(url,TEduBoardController.TEduBoardImageFitMode.TEDU_BOARD_IMAGE_FIT_MODE_CENTER);
                }
            }
        }

        @Override
        public void onTEBDeleteBoard(List<String> boardId, final String fileId) {
            LogUtils.i("onTEBDeleteBoard:" + fileId + "|" + boardId);
        }

        @Override
        public void onTEBGotoBoard(String boardId, final String fileId) {
            LogUtils.i("onTEBGotoBoard:" + fileId + "|" + boardId);
        }

        @Override
        public void onTEBGotoStep(int currentStep, int total) {
            LogUtils.i("onTEBGotoStep:" + currentStep + "|" + total);
        }

        @Override
        public void onTEBRectSelected() {
            LogUtils.i("onTEBRectSelected:");
        }

        @Override
        public void onTEBRefresh() {
            LogUtils.i("onTEBRefresh:");
        }

        @Override
        public void onTEBDeleteFile(String fileId) {
        }

        @Override
        public void onTEBSwitchFile(String fileId) {
        }

        @Override
        public void onTEBAddTranscodeFile(String s) {
            LogUtils.i("onTEBAddTranscodeFile:" + s);
        }

        @Override
        public void onTEBUndoStatusChanged(boolean canUndo) {

        }

        @Override
        public void onTEBRedoStatusChanged(boolean canredo) {

        }

        @Override
        public void onTEBFileUploadProgress(final String path, int currentBytes, int totalBytes, int uploadSpeed, float percent) {
            LogUtils.i("onTEBFileUploadProgress:" + path + " percent:" + percent);
        }

        @Override
        public void onTEBFileUploadStatus(final String path, int status, int code, String statusMsg) {
            LogUtils.i("onTEBFileUploadStatus:" + path + " status:" + status);
        }

        @Override
        public void onTEBFileTranscodeProgress(String s, String s1, String s2, TEduBoardController.TEduBoardTranscodeFileResult tEduBoardTranscodeFileResult) {

        }

        @Override
        public void onTEBH5FileStatusChanged(String fileId, int status) {

        }

        @Override
        public void onTEBAddImagesFile(String fileId) {
            LogUtils.i("onTEBAddImagesFile:" + fileId);
        }

        @Override
        public void onTEBVideoStatusChanged(String fileId, int status, float progress, float duration) {
            LogUtils.i("onTEBVideoStatusChanged:" + fileId + " | " + status + "|" + progress);
        }

        @Override
        public void onTEBSnapshot(String path, int code, String msg) {
            LogUtils.i("onTEBSnapshot:" + path + " | " + code + "|" + msg);
        }

        @Override
        public void onTEBH5PPTStatusChanged(int statusCode, String fid, String describeMsg) {

        }

        @Override
        public void onTEBImageStatusChanged(String boardId, String url, int status) {
            LogUtils.i("onTEBImageStatusChanged:" + boardId + "|" + url + "|" + status);
        }

        @Override
        public void onTEBSetBackgroundImage(final String url) {
            LogUtils.i("onTEBSetBackgroundImage:" + url);
        }

        @Override
        public void onTEBAddImageElement(final String url) {
            LogUtils.i("onTEBAddImageElement:" + url);
        }

        @Override
        public void onTEBAddElement(String id, String url) {

        }

        @Override
        public void onTEBBackgroundH5StatusChanged(String boardId, String url, int status) {
            LogUtils.i("onTEBBackgroundH5StatusChanged:" + boardId + " url:" + boardId + " status:" + status);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == Common.TakePhotoRequestCode) {
            if (takePhotoDialog != null) {
                String localPath = takePhotoDialog.getTakePhotoPath();
                if (!TextUtils.isEmpty(localPath)) {
                    mBoard.setBackgroundImage(localPath, TEduBoardController.TEduBoardImageFitMode.TEDU_BOARD_IMAGE_FIT_MODE_CENTER);
                }
            }
        } else if (requestCode == Common.PhotoAlbumRequestCode) {
            if (data != null) {
                Uri uri = data.getData();
                String localPath = ImageUtils.uriToRealPath(this, uri);
                if (!TextUtils.isEmpty(localPath)) {
                    mBoard.setBackgroundImage(localPath, TEduBoardController.TEduBoardImageFitMode.TEDU_BOARD_IMAGE_FIT_MODE_CENTER);
                }
            }
        }
    }

    private void stopTimeCount() {
        mTimeHandler.removeCallbacks(mTimeRunnable);
        mTimeRunnable = null;
    }

    @Override
    protected void onDestroy() {
        mTicManager.removeEventListener(this);
        if (mBoard != null) {
            View boardview = mBoard.getBoardRenderView();
            if (broadFl != null && boardview != null) {
                broadFl.removeView(boardview);
            }
            mBoard.uninit();
        }
        stopTimeCount();
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    public static class ToolType {
        ToolType(String name, int type) {
            this.name = name;
            this.type = type;
        }

        public String name;
        public int type;
    }
}
